package com.kylin.system.controller;

import com.kylin.common.core.beans.ResultBean;
import com.kylin.common.core.entity.DataEntity;
import com.kylin.common.core.entity.Page;
import com.kylin.common.core.validator.Assert;
import com.kylin.common.core.validator.BeanValidator;
import com.kylin.common.log.annotation.Log;
import com.kylin.common.log.enums.BusinessType;
import com.kylin.system.entity.SysDictType;
import com.kylin.system.service.SysDictTypeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 数据字典信息
 * 
 * @author wuhao
 */
@RestController
@RequestMapping("/dict/type")
public class SysDictTypeController
{
    /** 创建时间数据库字段 */
    private static final String  CREATE_TIME_FILED = "create_time";

    @Autowired
    private SysDictTypeService dictTypeService;

//    @PreAuthorize("@ss.hasPermi('system:dict:list')")
    @GetMapping("/list")
    public ResultBean<Page<SysDictType>> list(SysDictType dictType)
    {
        // 构造搜索
        dictType.getQueryWrapper()
                .like(StringUtils.isNotBlank(dictType.getDictName()), "dict_name", dictType.getDictName())
                .like(StringUtils.isNotBlank(dictType.getDictType()),"dict_type", dictType.getDictType())
                .like(StringUtils.isNotBlank(dictType.getStatus()),"status", dictType.getStatus());

        Page<SysDictType> page = new Page<>();
        page = dictTypeService.listPage(page, dictType);
        return ResultBean.onSuccess(page);
    }

    /**
     * 查询字典类型详细
     */
//    @PreAuthorize("@ss.hasPermi('system:dict:query')")
    @GetMapping(value = "/{dictId}")
    public ResultBean<SysDictType> getInfo(@PathVariable Long dictId)
    {
        // 非空校验
        Assert.isBlank("查询编号不能为空", dictId);

        SysDictType sysDictType = dictTypeService.getById(dictId);
        return ResultBean.onSuccess(sysDictType);
    }

    /**
     * 新增字典类型
     */
//    @PreAuthorize("@ss.hasPermi('system:dict:add')")
    @Log(title = "字典类型", businessType = BusinessType.INSERT)
    @PostMapping
    public ResultBean<Object> add(@RequestBody SysDictType dict)
    {
        // 参数校验
        BeanValidator.validate(dict);
        dictTypeService.insertDictType(dict);
        return ResultBean.onSuccess();
    }

    /**
     * 修改字典类型
     */
//    @PreAuthorize("@ss.hasPermi('system:dict:edit')")
    @Log(title = "字典类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResultBean<Object> edit(@RequestBody SysDictType dict)
    {
        // 参数校验
        BeanValidator.validate(dict);
        dictTypeService.updateDictType(dict);
        return ResultBean.onSuccess();
    }

    /**
     * 删除字典类型
     */
//    @PreAuthorize("@ss.hasPermi('system:dict:remove')")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictIds}")
    public ResultBean<Object> remove(@PathVariable Long[] dictIds)
    {
        // 校验
        Assert.isBlank("请选择需要删除的数据", dictIds);
        int count = dictTypeService.deleteDictTypeByIds(dictIds);
        return ResultBean.onSuccess().put("count", count);
    }

    /**
     * 清空缓存
     */
//    @PreAuthorize("@ss.hasPermi('system:dict:remove')")
    @Log(title = "字典类型", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clearCache")
    public ResultBean<Object> clearCache()
    {
        dictTypeService.clearCache();
        return ResultBean.onSuccess();
    }

    /**
     * 获取字典选择框列表
     */
    @GetMapping("/optionselect")
    public ResultBean<List<SysDictType>> optionSelect()
    {
        // 选择未禁用的数据
        SysDictType where = new SysDictType();
        where.setStatus(DataEntity.STATUS_NORMAL);
        List<SysDictType> dictTypes = dictTypeService.list(where);
        return ResultBean.onSuccess(dictTypes);
    }

}
